﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ACCESS连接
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Enabled = false;
            textBox2.Enabled = false;
        }
        string path = "";
        DataTable dt = new DataTable();

        private void button1_Click(object sender, EventArgs e)
        {

            //选择数据源
            FolderBrowserDialog file = new FolderBrowserDialog();
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = String.Format("文件（*.mdb)|*.mdb;*.MDB");
            DialogResult result = openfile.ShowDialog();
            if (result == DialogResult.OK)
            {
                path = Path.GetFullPath(openfile.FileName);
                textBox1.Text = path;
            }


            AccessHelper.ConnString = @"Provider=Microsoft.jet.oledb.4.0;Data Source=C:\Users\1032019725\Desktop\2015XJFDDY.mdb;Jet OLEDB:Database Password=";
            bool res = AccessHelper.TestConn();
            if (res)
            {
                string eror = "";
                dt = AccessHelper.GetDataTable(out eror, "select YXTCHD , BCTRZD, PMGX,YZHCD,TRYJZHL,TRSJD,ZACJDBSD,PSTJ,DXPD,GGBZL,DBYSLTD,TMPD,GGSY from  XJFDDY", @"Provider=Microsoft.jet.oledb.4.0;Data Source=" + path + ";Jet OLEDB:Database Password=");
                if(eror == "")
                {
                    dataGridView1.DataSource = null;
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show(eror);
                
                }
            }
           
        }
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="exp_path">导出路径</param>
        private void WriteDatatableToFile(DataTable dt, string exp_path)
        {
            FileStream fs = new FileStream(exp_path + "\\data.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //遍历表格
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string content = "";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string name = dt.Columns[j].ColumnName;
                    content += name + ":" + dt.Rows[i][name].ToString() + ",";
                }
                content = content.Substring(0, content.Length - 1);
                content += "\n";
                //写入数据
                sw.Write(content);
                //清空缓冲区
                sw.Flush();
            }

            //关闭流
            sw.Close();
            fs.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog file = new FolderBrowserDialog();
            if (file.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = file.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
                string exp_path = textBox2.Text.Trim();
                if (exp_path == "")
                {
                    MessageBox.Show("提示", "路径不能为空！");
                }
                else
                {
                    WriteDatatableToFile(dt, exp_path);
                    MessageBox.Show("success");
                }

            

        }
    }
}
